//
//  CRYWorkWX.m
//  workWX
//
//  Created by 谢锷 on 2020/5/8.
//  Copyright © 2020 rongyiqiche Data and Technology Shanghai Co. Ltd. All rights reserved.
//

#import "CRYWorkWX.h"
#import "work_wx_define.h"
#import "NSDictionaryUtils.h"
#import "UZAppDelegate.h"
#import "WWKApi.h"
#import "CRYTool.h"
#import "CRYShareManager.h"
#import "CRYWWApiManager.h"

@interface CRYWorkWX ()<UIApplicationDelegate, WWKApiDelegate>


@end

@implementation CRYWorkWX

+ (void)onAppLaunch:(NSDictionary *)launchOptions {
    // 方法在应用启动时被调用
    NSDictionary *feature   = [[UZAppDelegate appDelegate] getFeatureByName:@"workWX"];
    NSString *appId         = [feature stringValueForKey:@"scheme" defaultValue:nil];
    NSString *corpId        = [feature stringValueForKey:@"corpId" defaultValue:nil];
    NSString *agentId       = [feature stringValueForKey:@"agentId" defaultValue:nil];
    if ([CRYTool isContentNull:appId] || [CRYTool isContentNull:appId] || [CRYTool isContentNull:appId]) {
        [CRYWWApiManager registerApp:@"wwauth4ce5dfeea2e0bc710000021" corpId:@"ww4ce5dfeea2e0bc712" agentId:@"10000021"];
    } else {
        [CRYWWApiManager registerApp:appId corpId:corpId agentId:agentId];
    }
}

- (id)initWithUZWebView:(UZWebView *)webView {
    if (self = [super initWithUZWebView:webView]) {
        // 初始化方法
        [theApp addAppHandle:self];
    }
    return self;
}

- (void)dispose {
    // 方法在模块销毁之前被调用
    [theApp removeAppHandle:self];
}

#pragma mark - UIApplicationDelegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [self handleOpenURL:url sourceApplication:nil];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation {
    return [self handleOpenURL:url sourceApplication:sourceApplication];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
    return [self handleOpenURL:url sourceApplication:nil];
}

- (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
    /*! @brief 处理外部调用URL的时候需要将URL传给SDK进行相关处理
     * @param url 外部调用传入的url
     * @param delegate 当前类需要实现WWKApiDelegate对应的方法
     */
    return [WWKApi handleOpenURL:url delegate:self];
}

- (void)onResp:(WWKBaseResp *)resp {
    
    //    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    //    [dic setValue:@(resp.errCode) forKey:@"code"];
    //    [dic setValue:resp.errStr forKey:@"msg"];
    //    if (self.context) {
    //        [self.context callbackWithRet:dic err:nil delete:YES];
    //    }
}

- (void)onRespWithContext:(UZModuleMethodContext *)context result:(BOOL) result {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:result ? @(CRYWorkWXCode_Success) : @(CRYWorkWXCode_Error) forKey:@"code"];
    [dic setValue:result ?  CRYWorkWX_Share_Suc : CRYWorkWX_Share_Error forKey:@"msg"];
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
}

#pragma mark - js methods

JS_METHOD(openApp:(UZModuleMethodContext *)context) {

    NSDictionary *dic = [CRYWWApiManager openApp];
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
}

JS_METHOD(isInstalled:(UZModuleMethodContext *)context) {
    
    NSDictionary *dic = [CRYWWApiManager isInstalled];
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
}

/**
 普通方法，方法会在主线程执行，结果通过回调的方式回传js，方法名以jsmethod_作为前缀，如：- (void)jsmethod_registerApp:(UZModuleMethodContext *)context，为了方便一般使用JS_METHOD宏来定义
 */
JS_METHOD(registerApp:(UZModuleMethodContext *)context) {
    
    NSDictionary *param     = context.param;
    NSString *appId         = [param stringValueForKey:@"scheme" defaultValue:nil];
    NSString *corpId        = [param stringValueForKey:@"corpId" defaultValue:nil];
    NSString *agentId       = [param stringValueForKey:@"agentId" defaultValue:nil];
    NSDictionary *feature   = [self getFeatureByName:@"workWX"];
    if ([CRYTool isContentNull:appId]) {
        appId = [feature stringValueForKey:@"scheme" defaultValue:nil];
    }
    if ([CRYTool isContentNull:corpId]) {
        corpId = [feature stringValueForKey:@"corpId" defaultValue:nil];
    }
    if ([CRYTool isContentNull:agentId]) {
        agentId = [feature stringValueForKey:@"agentId" defaultValue:nil];
    }
    NSDictionary *dic = [CRYWWApiManager registerApp:appId corpId:corpId agentId:agentId];
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
}

JS_METHOD(shareText:(UZModuleMethodContext *)context) {
    
    NSDictionary *param     = context.param;
    NSString *text          = [param stringValueForKey:@"text" defaultValue:nil];
    NSDictionary *dic = [CRYShareManager shareText:text];
    
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
}

JS_METHOD(shareImage:(UZModuleMethodContext *)context) {
    
    NSDictionary *param     = context.param;
    NSString *imgName       = [param stringValueForKey:@"imgName" defaultValue:nil];
    NSString *imgPath       = [param stringValueForKey:@"imgPath" defaultValue:nil];
    imgPath = [self getPathWithUZSchemeURL:imgPath];
    
    NSDictionary *dic = [CRYShareManager shareImage:imgName imgPath:imgPath];
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
    
}

JS_METHOD(shareFile:(UZModuleMethodContext *)context) {
    
    NSDictionary *param     = context.param;
    NSString *fileName      = [param stringValueForKey:@"fileName" defaultValue:nil];
    NSString *filePath      = [param stringValueForKey:@"filePath" defaultValue:nil];
    filePath = [self getPathWithUZSchemeURL:filePath];
    NSDictionary *dic = [CRYShareManager shareFile:fileName filePath:filePath];
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
}

JS_METHOD(shareVideo:(UZModuleMethodContext *)context) {
    
    NSDictionary *param     = context.param;
    NSString *videoName     = [param stringValueForKey:@"videoName" defaultValue:nil];
    NSString *videoPath     = [param stringValueForKey:@"videoPath" defaultValue:nil];
    videoPath = [self getPathWithUZSchemeURL:videoPath];
    
    NSDictionary *dic = [CRYShareManager shareVideo:videoName videoPath:videoPath];
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
}

JS_METHOD(shareLink:(UZModuleMethodContext *)context) {
    
    NSDictionary *param     = context.param;
    NSString *title         = [param stringValueForKey:@"title" defaultValue:nil];
    NSString *summary       = [param stringValueForKey:@"summary" defaultValue:nil];
    NSString *url           = [param stringValueForKey:@"url" defaultValue:nil];
    NSString *iconUrl       = [param stringValueForKey:@"iconUrl" defaultValue:nil];
    iconUrl = [self getPathWithUZSchemeURL:iconUrl];
    
    NSDictionary *dic = [CRYShareManager shareLink:title summary:summary url:url iconUrl:iconUrl];
    if (context) {
        [context callbackWithRet:dic err:nil delete:YES];
    }
}

@end
